﻿using Marbles.CMS.Core;
using Marbles.Core.Web.SiteMapManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using Marbles.CMS.ContentTypes;

namespace Marbles.CMS.Administrator.Content
{
    public partial class ArticleList : ContentEditorBase
    {



        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "Articles";

            if (!Page.IsPostBack)
            {
                ReloadForm();
            }
        }

        private TreeNode LoadContentTree(SiteMapNode root)
        {
            TreeNode node = new TreeNode();
            node.Text = root.Title;
            node.Value = root.Url;
            node.ToolTip = root.Title;
            if (root.HasChildNodes)
            {
                foreach (SiteMapNode childnode in root.ChildNodes)
                {
                    node.ChildNodes.Add(LoadContentTree(childnode));
                }
            }

            return node;
        }

        protected void treeContent_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView caller = sender as TreeView;
            ContentManagerBase cntlr = new ContentManagerBase();
            PageRenderer renderer = cntlr.GetPageRenderer(caller.SelectedNode.Value);

            Response.Redirect(string.Format("?action=Edit.{0}&cid={1}", renderer.PageType, caller.SelectedNode.Value), true);
        }

        private void ReloadForm()
        {
            ContentTypes.Tasks.ContentManager mgr = new ContentTypes.Tasks.ContentManager();
            this.treeContent.Nodes.Clear();
            this.treeContent.Nodes.Add(LoadContentTree(new SiteMapReader("/Web.sitemap").Nodes));
            this.lstTasks.DataSource = mgr.GetTasks(Page.User.Identity.Name, true);
            this.lstTasks.DataBind();
        }

        protected void lstTasks_itemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((e.Item.ItemType != ListItemType.Item) && (e.Item.ItemType != ListItemType.AlternatingItem)) { return; }
            DateTime current = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            Button btnEdit = e.Item.FindControl("btnEdit") as Button;
            Button btnCancel = e.Item.FindControl("btnCancel") as Button;
            Button btnComplete = e.Item.FindControl("btnComplete") as Button;
            Label lblSubject = e.Item.FindControl("lblSubject") as Label;

            btnEdit.OnClientClick = string.Format("window.location.href='Default.aspx?action=Task&taskid={0}'", lstTasks.DataKeys[e.Item.ItemIndex].ToString());
            btnCancel.OnClientClick = "if (confirm('Are you sure?')) {" + string.Format("window.location.href='Default.aspx?action=Task&subaction=cancel&taskid={0}';", lstTasks.DataKeys[e.Item.ItemIndex].ToString()) + "} else {return false;}";
            btnComplete.OnClientClick = string.Format("window.location.href='Default.aspx?action=Task&subaction=complete&taskid={0}'", lstTasks.DataKeys[e.Item.ItemIndex].ToString());

            ContentTypes.Tasks.TaskEntryItem item = e.Item.DataItem as ContentTypes.Tasks.TaskEntryItem;

            if (item.DueDate < current)
            {
                lblSubject.ForeColor = System.Drawing.Color.Red;
                lblSubject.Text += " (Overdue)";
            }

            if (item.DueDate == current)
            {
                lblSubject.ForeColor = System.Drawing.Color.Green;
                lblSubject.Text += " (Due Today)";
            }

            if (item.DueDate == current.AddDays(1))
            {
                lblSubject.ForeColor = System.Drawing.Color.Purple;
                lblSubject.Text += " (Due Tommorrow)";
            }

        }

    }
}